const db = require("../db/index");

// 添加文章
exports.addAticl = (req, res) => {
    const data = req.body;
    const addArticl = `insert into ev_articles set ?`;
    db.query(addArticl, data, (err, results) => {
        if (err) return res.mess(err);
        if (results.affectedRows !== 1) return res.mess("添加文章失败！");
        return res.mess("添加文章成功！", 200);
    });
};

// 根据id修改文章
exports.updateAticl = (req, res) => {
    const data = req.body;
    const addArticl = `update ev_articles set ? where id=?`;
    db.query(addArticl, [data, data.id], (err, results) => {
        if (err) return res.mess(err);
        if (results.affectedRows !== 1) return res.mess("修改文章失败！");
        return res.mess("修改文章成功！", 200);
    });
};

// 根据id获取文章信息
exports.getArticlByid = (req, res) => {
    const sql = "select * from ev_articles where id=?";
    db.query(sql, req.query.id, (err, results) => {
        if (err) return res.mess(err);
        if (results.length !== 1) return res.mess("获取文章信息失败！");
        return res.send({
            status: 200,
            message: "获取文章信息成功！",
            data: results[0],
        });
    });
};

// 获取所有文章信息
exports.getArticls = (req, res) => {
    const sql = `select * from ev_articles where is_delete=0 order by id asc`;
    db.query(sql, (err, results) => {
        if (err) return res.mess(err);
        return res.send({
            status: 200,
            message: "获取文章列表成功！",
            data: results,
        });
    });
};
// 根据id删除文章
exports.deleteArticlById = (req, res) => {
    const deleteCate = `update ev_articles set is_delete=1 where id=?`;
    db.query(deleteCate, req.query.id, (err, results) => {
        if (err) return res.mess(err);
        if (results.affectedRows !== 1) return res.mess("删除失败!");
        return res.mess("删除成功！", 200);
    });
};